﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace AutoUpgrade
{
    internal static class GlobalSetting
    {
        public static string LocalConfigFileName => "version.xml";

        public static string Suffix => "?rand=" + DateTime.Now.ToString("yyyyMMddHHmmss");

        public static string LocalXmlFilePath => Path.Combine(Environment.CurrentDirectory, LocalConfigFileName);

        /// <summary>
        /// 更新服务器的路径
        /// </summary>
        public static string Host
        {
            get
            {
                string xml = string.Empty;
                string fileName = System.IO.Path.Combine(Environment.CurrentDirectory, "Upgrade.config");
                if (System.IO.File.Exists(fileName))
                    xml = System.IO.File.ReadAllText(fileName);
                if (!string.IsNullOrEmpty(xml))
                {
                    XmlDocument doc = new XmlDocument();
                    doc.LoadXml(xml);
                    return doc.SelectSingleNode("arguments").SelectSingleNode("host").InnerText;
                }
                else
                    return string.Empty;
            }
            set
            {
                string fileName = System.IO.Path.Combine(Environment.CurrentDirectory, "Upgrade.config");
                string xml = string.Empty;
                if (System.IO.File.Exists(fileName))
                    xml = System.IO.File.ReadAllText(fileName);
                XmlDocument doc = new XmlDocument();
                if (!string.IsNullOrEmpty(xml))
                {
                    doc.LoadXml(xml);
                    doc.SelectSingleNode("arguments").SelectSingleNode("host").InnerText = value;
                }
                else
                {
                    var arguments = doc.CreateElement("arguments");
                    var host = doc.CreateElement("host");
                    var startup = doc.CreateElement("startup");
                    arguments.AppendChild(host);
                    arguments.AppendChild(startup);
                    doc.AppendChild(arguments);
                    host.InnerText = value;
                }
                if (System.IO.File.Exists(fileName))
                    System.IO.File.Delete(fileName);
                System.IO.File.WriteAllText(fileName, doc.OuterXml);
                //FileInfo info = new FileInfo(fileName);
                //if (info.Exists)
                //    info.Attributes = FileAttributes.Hidden;
            }
        }

        /// <summary>
        /// 主程序的文件名
        /// </summary>
        public static string StartupApplication
        {
            get
            {
                string fileName = System.IO.Path.Combine(Environment.CurrentDirectory, "Upgrade.config");
                string xml = string.Empty;
                if (System.IO.File.Exists(fileName))
                    xml = System.IO.File.ReadAllText(fileName);
                if (!string.IsNullOrEmpty(xml))
                {
                    XmlDocument doc = new XmlDocument();
                    doc.LoadXml(xml);
                    return doc.SelectSingleNode("arguments").SelectSingleNode("startup").InnerText;
                }
                else
                    return string.Empty;
            }
            set
            {
                string xml = string.Empty;
                XmlDocument doc = new XmlDocument();
                string fileName = System.IO.Path.Combine(Environment.CurrentDirectory, "Upgrade.config");
                if (System.IO.File.Exists(fileName))
                    xml = System.IO.File.ReadAllText(fileName);
                if (!string.IsNullOrEmpty(xml))
                {
                    doc.LoadXml(xml);
                    doc.SelectSingleNode("arguments").SelectSingleNode("startup").InnerText = value;
                }
                else
                {
                    var arguments = doc.CreateElement("arguments");
                    var host = doc.CreateElement("host");
                    var startup = doc.CreateElement("startup");
                    arguments.AppendChild(host);
                    arguments.AppendChild(startup);
                    startup.InnerText = value;
                }
                if (System.IO.File.Exists(fileName))
                    System.IO.File.Delete(fileName);
                System.IO.File.WriteAllText(fileName, doc.OuterXml);
                //FileInfo info = new FileInfo(fileName);
                //if (info.Exists)
                //    info.Attributes = FileAttributes.Hidden;
            }
        }

        /// <summary>
        /// 本地配置文件
        /// </summary>
        public static XmlVersionConfig LocalConfig { get; set; }
        /// <summary>
        /// 远程配置文件
        /// </summary>
        public static XmlVersionConfig ServerConfig { get; set; }
    }

}
